/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.event;

import java.io.Serializable;

import x10.enums.Security;

/**
 *
 * @author Denny
 */
public class X10SecurityCommand extends X10Event implements Serializable {
    static final long serialVersionUID = 1L;
    Security command;
    private String address;
    private String source;
    private long instantiatedMillis;

    public X10SecurityCommand(String securityCode){
        this.instantiatedMillis = System.currentTimeMillis();
        this.address =securityCode;
    }

    public X10SecurityCommand(String securityCode, Security command, String source){
	this(securityCode);
        this.command = command;
        this.source = source;
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the instantiatedMillis
     */
    public long getInstantiatedMillis() {
        return instantiatedMillis;
    }

    /**
     * @param instantiatedMillis the instantiatedMillis to set
     */
    public void setInstantiatedMillis(long instantiatedMillis) {
        this.instantiatedMillis = instantiatedMillis;
    }

    public Security getCommand() {
        return command;
    }

    public void setCommand(Security command) {
        this.command = command;
    }

    public boolean equals(X10Event device){
        boolean isEqual = true;

        if(device instanceof X10SecurityCommand){
            X10SecurityCommand other = (X10SecurityCommand)device;
            if(other!=null){
                if(!this.getAddress().contentEquals(other.getAddress())){
                    isEqual = false;
                }
                if(!this.getSource().contentEquals(other.getSource())){
                    isEqual = false;
                }
                if(this.command != other.getCommand()){
                    isEqual = false;
                }

                //If both objects are within 1 second of each other, call them equal
                long low = this.getInstantiatedMillis() - 1000; //1 second earlier
                long high = this.getInstantiatedMillis() + 1000; //1 second later
                long otherMillis = other.getInstantiatedMillis();

                if(!((otherMillis >= low) && (otherMillis <= high))){
                    isEqual = false;
                }
            }
        } else {
            isEqual = false;
        }

        return isEqual;
    }

    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("X10 Security Command ID: ");
        sb.append(address);
        sb.append(" Command: ");
        sb.append(command.toString());
        sb.append(" Source: ");
        sb.append(source);
        return sb.toString();
    }
}
